package com.tx.admin.security.service.impl;

import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.util.ByteSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.tx.admin.modules.rbac.entity.UserEntity;
import com.tx.admin.security.service.Md5Service;
import com.tx.admin.util.common.UUIDUtils;


@Component
@ConfigurationProperties(prefix = "hash")
class Md5ServiceImpl extends DefaultHashService implements Md5Service{


	public String hashString(String str,Object salt) {
	
		HashRequest request = new HashRequest.Builder()  
	           .setSource(ByteSource.Util.bytes(str))  
	   
	           .setSalt(salt)
	           
	           .build();
		return this.computeHash(request).toString();
	}
	
	public UserEntity hashUser(UserEntity entity) {
		if(entity.getSalt() == null) {
			entity.setSalt(UUIDUtils.getId(this.getClass()));
		}
		String password=this.hashString(entity.getPassword(), entity.getSalt());
		entity.setPassword(password);
		return entity;
	}


}
